/**
* @file         chmotor_modbus_rtu.h
* @author       David Hu (hmd_hubei_cn@163.com)
* @brief         
* @version      0.1
* @date         2024.09.12
* @note          
* @copyright    Copyright (c) 2022 DAVID HU All rights reserved. Licensed under the MIT License (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License in the file LICENSE

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.
**/

#ifndef __CHMOTOR_MODBUS_RTU_H__
#define __CHMOTOR_MODBUS_RTU_H__

#include <stdint.h>
#include <stdbool.h>
#include <string.h>

#ifdef __cplusplus
extern "C"{
#endif

#define CHMOTOR_DEV_SALVE_ADDR_M1 0x01  // modbus rtu 从机地址

#define CHMOTOR_DEV_SALVE_ADDR_M2 0x02

#define CHMOTOR_DEV_SALVE_ADDR_M3 0x03

#define CHMOTOR_DEV_SALVE_ADDR_M4 0x04

#define CHMOTOR_DEV_SALVE_ADDR_M5 0x05

#define CHMOTOR_DEV_SALVE_ADDR_M6 0x06

#define CHMOTOR_DEV_SALVE_ADDR_M7 0x07

#define CHMOTOR_DEV_SALVE_ADDR_M8 0x08

#define CHMOTOR_MODBUS_READ_REG_CMD 0x04  // modbus rtu 读寄存器指令

#define CHMOTOR_MODBUS_WRITE_REG_CMD 0x06  // modbus rtu 单独，写寄存器指令

#define CHMOTOR_MODBUS_WRITE_REG_CMD_MULTI 0x10  // modbus rtu 多个，写寄存器指令

#define CHMOTOR_REG_ADDR_BUSBARVOL_R  0x0002 // 读,推进器母线电压寄存器地址

#define CHMOTOR_REG_ADDR_SPEED_W 0x0019 // 写，推进器速度寄存器地址，正负表示正反转，绝对值表示速度大小，单位rpm

bool chmotor_modbus_read_register(uint8_t slave_addr, uint16_t start_reg_addr, uint16_t reg_num);

bool chmotor_modbus_write_register(uint8_t slave_addr, uint16_t reg_addr, uint8_t* data, uint8_t data_len);

bool chmotor_modbus_write_multi_register(uint8_t slave_addr, uint16_t start_reg_addr, uint16_t reg_num, uint8_t* data, uint8_t data_len);

uint8_t* chmotor_modbus_pack_get_packbuff(void);

uint8_t chmotor_modbus_pack_get_packlen(void);

uint8_t chmotor_modbus_unpack_get_datalen(void);

uint8_t* chmotor_modbus_unpack_get_databuff(void);

uint8_t chmotor_modbus_unpack_get_slave_addr(void);

bool chmotor_modbus_parse_slave_ack_read(uint8_t in_data);




#ifdef __cplusplus
}
#endif


#endif //__CHMOTOR_MODBUS_RTU_H__
/********************* (C) COPYRIGHT DAVID HU *******END OF FILE ********/
